/*  Blackjack "soft 17" Game Dealer routine
    Author:	Roberto Ontiveros Vazquez 
		Wendy Ly
		Fernando Fonseca Garcia
*/
#include <stdio.h>
#include<stdlib.h>

char cardpack[52];
char crdpckshffle[52];
char cardsDealer[11];
char printcrdsD[22];
char cardsValue[13]={'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
int pckshfflindex=0;
int numcardDealer=0, Dscore=0;
int acesoftD=0, cardTaken=0;


/* Assigning value to a card taken from the pack*/
int getCardV(int index)
{
	int value=0, subindex=0;

	subindex=crdpckshffle[index];

	if((subindex<27) && (subindex>13))
		subindex=subindex-13;
	else if((subindex<40) && (subindex>26))
		subindex=subindex-26;
	else if((subindex<53) && (subindex>39))
		subindex=subindex-39;

	if((subindex>1) && (subindex<10))
		value=subindex;
	else if(subindex>9)
		value=10;
	else if(subindex==1)
	{
		value=11;
		acesoftD++;	
	}

	pckshfflindex++;
	
	cardsDealer[numcardDealer]=crdpckshffle[index];
	numcardDealer++;
	

	return value;
}

/* Calculating score while keeping in mind aces soft values*/
int calcSHScore(int value)
{
	int tempscore, finalscore,acess;

	tempscore=Dscore+value;
	acess=acesoftD;

	if((tempscore>21)&&(acess>0))
	{
		finalscore=tempscore-10;
		acesoftD--;
	
	}
	else
		finalscore=tempscore;

	return finalscore;
}
/* Assigning suit and value for printing the hand.
Arrangement:clubs, spades, hearts, diamonds*/
void gethandCrdSuit()
{
	int i,j=0,subindex;
	for(i=0;i<11;i++)
	{
		subindex=cardsDealer[i];
		if(subindex==0);
		else
		{
			if((subindex<27) && (subindex>13))
			{
				subindex=subindex-13;
				printcrdsD[j+1]='s';
			}
			else if((subindex<40) && (subindex>26))
			{
				subindex=subindex-26;
				printcrdsD[j+1]='h';
			}
			else if((subindex<53) && (subindex>39))
			{
				subindex=subindex-39;
				printcrdsD[j+1]='d';
			}
			else
				printcrdsD[j+1]='c';
			printcrdsD[j]=cardsValue[subindex-1];
			j=j+2;
		}
	}
	
}


/* Main dealer soft 17 game programm*/
void main() 
{


	Dscore=acesoftD=numcardDealer=0;

	cardTaken=getCardV(pckshfflindex);
	Dscore=calcSHScore(cardTaken);
	cardTaken=getCardV(pckshfflindex);
	Dscore=calcSHScore(cardTaken);
		

	while(Dscore<17)
	{
		cardTaken=getCardV(pckshfflindex);
		Dscore=calcSHScore(cardTaken);
	}
		
	gethandCrdSuit();

}